package env_service

import (
	"bee/models"
	"bee/pkg/logging"
	"encoding/json"
)

type Env struct {
	Namespace string
	TraceId string
}

func (e Env) GetAppEnv(appName string) (*models.AppEnv, error) {
	queryMap := e.getMaps()
	queryMap["app_name"] = appName
	queryMap["app_namespace"] = e.Namespace

	env, err := models.GetEnv(queryMap)
	if err != nil {
		return nil, err
	}

	return env, nil
}

func (e Env) GetAllAppEnv() (string, error) {
	queryMap := e.getMaps()
	queryMap["app_namespace"] = e.Namespace

	envs, err := models.GetEnvs(queryMap)
	if err != nil {
		return "", err
	}

	logging.ZapLogger.Infow("环境信息", "envs", envs, "traceId", e.TraceId)
	if len(envs) == 0 {
		return "", nil
	}

	data := make(map[string]interface{})

	for _, v := range envs {
		data[v.AppName] = v

	}

	jsonByte, jsonErr := json.Marshal(data)
	if jsonErr != nil {
		return "", jsonErr
	}

	jsonStr := string(jsonByte)
	logging.ZapLogger.Infow("数据类型装换", "jsonStr", jsonStr, "traceId", e.TraceId)
	return jsonStr, nil

}


func (e Env) getMaps() map[string]interface{} {
	maps := make(map[string]interface{})

	maps["is_deleted"] = 0

	return maps
}